﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace HY.Web.Areas.System.Controllers
{
    public class SysModuleController : BaseController
    {
        SysModuleService service = new SysModuleService();
        // GET: System/SysMenu
        public ActionResult Index()
        {
            //Expression<Func<SysMenu, bool>> where = (m => m.IsDeleted == false);
            //where = where.And(m => m.CreateDate < DateTime.Now);
            //List<OrderByExpression> order = new List<OrderByExpression>();
            //order.Add(new OrderByExpression() { PropertyName = PropertySupport.GetPropertyName<SysMenu>(a => a.MenuSort) });
            //order.Add(new OrderByExpression() { PropertyName = PropertySupport.GetPropertyName<SysMenu>(a => a.CreateDate), IsDesc = true });
            //var pageList = service.GetPageList(where, 1, 3, order.ToArray());
            return View();
        }

        /// <summary>
        /// 获取菜单树列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List()
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var list = GlobalSupport.MyModules;
                returnModel = base.GetReturnModel(list.Count, list);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("获取菜单树", ex);
            }
            return Json(returnModel);
        }

        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Add(SysModule model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Insert };
            try
            {
                model.CreateUserID = base.UserInfo.UserID;
                model.CreateDate = DateTime.Now;
                returnModel.count = service.Add(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Info(logInfo);
                //清除模块缓存，以加载最新模块信息
                WebHelper.RemoveCache(GlobalKey.Module);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Edit(int? id)
        {
            var model = service.GetById<SysModule>(id);
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(SysModule model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Update };
            try
            {
                var oldModel = service.GetById<SysModule>(model.ID);
                model.IsDeleted = oldModel.IsDeleted;
                model.CreateDate = oldModel.CreateDate;
                model.CreateUserID = oldModel.CreateUserID;
                model.LastModifyUserID = base.UserInfo.UserID;
                model.LastModifyDate = DateTime.Now;
                returnModel.count = service.Update(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Info(logInfo);
                //清除模块缓存，以加载最新模块信息
                WebHelper.RemoveCache(GlobalKey.Module);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        [HttpPost]
        public JsonResult Delete(int? id)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Delete };
            try
            {
                returnModel.count = service.Delete<SysModule>(id);
                returnModel.msg = "删除成功";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Info(logInfo);
                //清除模块缓存，以加载最新模块信息
                WebHelper.RemoveCache(GlobalKey.Module);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "删除失败";
                logInfo.LogMessage = $"菜单{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        /// <summary>
        /// 获取Select-V4所需数据格式
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public JsonResult MenuSelects()
        {
            Dictionary<string, string> actionInfo = new Dictionary<string, string>();
            var menuList = GlobalSupport.MyMenus;
            List<V_FormSelects> v_FormSelectsList = ChildrenSelects(0);
            return Json(v_FormSelectsList);
            
            List<V_FormSelects> ChildrenSelects(int parentID)
            {
                List<V_FormSelects> childrenList = new List<V_FormSelects>();
                foreach (var module in menuList.Where(m => m.ParentID == parentID))
                {
                    V_FormSelects select = new V_FormSelects();
                    select.name = module.ModuleName;
                    select.value = module.ID;
                    select.children = ChildrenSelects(module.ID);
                    childrenList.Add(select);
                }
                return childrenList;
            }
        }

        #region 私有方法
        /// <summary>
        /// 循环生成Select-V4多级数据格式
        /// </summary>
        /// <param name="moduleList"></param>
        /// <param name="parentID"></param>
        /// <returns></returns>

        //private List<V_FormSelects> MenuSelects(List<SysModule> moduleList, int parentID)
        //{
        //    List<V_FormSelects> v_FormSelectsList = new List<V_FormSelects>();
        //    foreach (var module in moduleList.Where(m => m.ParentID == parentID))
        //    {
        //        V_FormSelects select = new V_FormSelects();
        //        select.name = module.ModuleName;
        //        select.value = module.ID;
        //        select.children = MenuSelects(moduleList, module.ID);
        //        v_FormSelectsList.Add(select);
        //    }
        //    return v_FormSelectsList;
        //}

        #endregion
    }
}